import axios from 'axios' // 引入axios
// import { clearLoginInfo } from '@/plugins/common'
import router from '@/router'

axios.defaults.headers['Content-Type'] = 'application/json;charset=utf-8'
// 创建axios实例
const http = axios.create({
  timeout: 1000 * 60, // 超时
})

// 请求拦截
http.interceptors.request.use(config => {
  // config.headers.Authorization = 'Bearer' || ''
  if(config.method === 'post'){
    const requestObj = {
      url: config.url,
      data: typeof config.data === 'object' ? JSON.stringify(config.data) : config.data,
      time: new Date().getTime()
    }
  }
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截
http.interceptors.response.use(response => {
  const code = response.data.code || 200
  // if(response.status == 200 && code == 9001){
  //   // clearLoginInfo() //清除用户信息 去登录
  //   router.replace({ name: 'login' })
  // }
  return response.data
}, 
error => {
  return Promise.reject(error)
})

export default http